package com.ljm.file.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljm.common.page.PageDto;
import com.ljm.file.record.dto.SysFileDto;
import com.ljm.file.record.entity.SysFile;
import com.ljm.file.recycle.entity.SysFileRecycle;
import com.ljm.file.recycle.mapper.SysFileRecycleMapper;
import com.ljm.file.recycle.service.ISysFileRecycleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.file.recycle.api.SysFileRecycleApi;
import com.ljm.file.recycle.dto.SysFileRecycleDto;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.Watchable;
import java.time.LocalDateTime;

/**
 * <p>
 * 平台文件信息 服务实现类
 * </p>
 *
 * @author HuamingChen
 * @since 2020-09-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysFileRecycleServiceImpl extends ServiceImpl<SysFileRecycleMapper, SysFileRecycle>
        implements ISysFileRecycleService, SysFileRecycleApi {
    private static final MapperFacade MAPPER_FACADE;
    static {
        MapperFactory mapperFactory=new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(SysFileRecycle.class,SysFileRecycleDto.class).byDefault().register();
        mapperFactory.classMap(SysFile.class,SysFileRecycle.class).byDefault().register();
        MAPPER_FACADE=mapperFactory.getMapperFacade();
    }

    @Override
    public void addFileRecycle(@NonNull SysFile fileInfo) {
        SysFileRecycle recycleFile=new SysFileRecycle();
        MAPPER_FACADE.map(fileInfo,recycleFile);
        recycleFile.setDeleteTime(LocalDateTime.now());
        this.save(recycleFile);
    }

    @Override
    public SysFile recoveryFile(String ossFileId) {
        SysFileRecycle po = this.getOne(new LambdaQueryWrapper<SysFileRecycle>().eq(SysFileRecycle::getOssFileId, ossFileId));
        if(po==null){
            throw new RuntimeException("该文件无法恢复,找不到该文件");
        }
        SysFile fileInfo=new SysFile();
        MAPPER_FACADE.map(po,fileInfo);
        this.removeById(po.getId());
        return fileInfo;
    }

    @Override
    public SysFileRecycleDto getByOssFileId(String ossFileId) {
        SysFileRecycle po = this.getOne(new LambdaQueryWrapper<SysFileRecycle>().eq(SysFileRecycle::getOssFileId, ossFileId));
        if(po==null){
            throw new RuntimeException("获取文件失败");
        }
        SysFileRecycleDto dto=new SysFileRecycleDto();
        MAPPER_FACADE.map(po,dto);
        return dto;
    }

    @Override
    public PageDto<SysFileRecycleDto> getFileRecycleByPage(SysFileRecycleDto dto) {
        PageDto<SysFileRecycleDto> pageDto=new PageDto<>(dto.getPageNo(),dto.getPageSize());
        IPage<SysFileRecycle> page=new Page<>(dto.getPageNo(),dto.getPageSize());
        LambdaQueryWrapper<SysFileRecycle> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(dto.getFdSys())){
            wrapper.eq(SysFileRecycle::getFdSys,dto.getFdSys());
        }
        if(StringUtils.isNotEmpty(dto.getFdModule())){
            wrapper.eq(SysFileRecycle::getFdModule,dto.getFdModule());
        }
        if(StringUtils.isNotEmpty(dto.getFdSectionId())){
            wrapper.eq(SysFileRecycle::getFdSectionId,dto.getFdSectionId());
        }
        if(StringUtils.isNotEmpty(dto.getStoragePath())){
            wrapper.likeRight(SysFileRecycle::getStoragePath,dto.getStoragePath());
        }
        if(StringUtils.isNotEmpty(dto.getOssFileId())){
            wrapper.eq(SysFileRecycle::getOssFileId,dto.getOssFileId());
        }
        if(dto.getFileType()!=null){
            wrapper.eq(SysFileRecycle::getFileType,dto.getFileType());
        }
        this.page(page, wrapper);
        if(page.getTotal()>0){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(MAPPER_FACADE.mapAsList(page.getRecords(), SysFileRecycleDto.class));
        }
        return pageDto;
    }
}
